package com.sparrow.event.core;


import com.sparrow.event.annotation.EventReceiver;
import com.sparrow.event.api.EventBus;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import java.lang.reflect.Method;

public class EventAutoRegister implements BeanPostProcessor {

    private final EventBus eventBus;

    public EventAutoRegister(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        for (Method method : bean.getClass().getDeclaredMethods()) {
            if (method.isAnnotationPresent(EventReceiver.class)) {
                eventBus.register(bean, method);
            }
        }
        return bean;
    }
}

